<?php
namespace app\admin\controller;

/**
 * @name Student
 * @desc 学员管理类
 * @author Aninet Sat Jun 16 12:11:02 2018
 */
use app\admin\model\Student as StudentModel;
use \think\Validate;
use \think\Config;
class Student extends Base {
    /**
     * 学生列表页
     * 
     * @return 
     * @date Sat Jun 16 13:36:15 2018
     */
    public function index(){
        $get = $this->req->get();
        $rule = [
            'page' => 'require'
        ];
        $page = null;
        $validate = new Validate($rule);

        if($validate->check($get)){
            if($get['page'] == 1){
                $page = 0;
            } else {
                $page = $get['page'];
            }
        }else{
            $page = 0;
        }
        
        $model = new StudentModel;
        // $data = $model->limit($page*Config::get('paginate.list_rows'),Config::get('paginate.list_rows'))->select();
        // var_dump(Config::get('paginate.list_rows'));exit();
        $data = $model->paginate(Config::get('paginate.list_rows'),false);
        $this->assign('data',$data);
        // $this->assign('list',$list);
        return $this->fetch();
    }

    public function info(){
        $post = $this->req->post();
        $rule = [
            'id' => 'require|number'
        ];
        $validate = new Validate($rule);
        if($validate->check($post)){
            $model = new StudentModel;
            $student = $model->where(['stu_id'=>$post['id']])->find();
            if($student){
                $info['msg'] = '学生个人数据';
                $this->assign('data',$student);
                $info['data'] = $this->fetch();
                echo $this->right($info);
            }else{
                $info['msg'] = "没有数据";
                echo $this->wrong($info);
            }
        }else{
            $info['msg'] = '系统错误';
            echo $this->wrong($info);
        }
        return;
    }

    /**
     * 添加学生
     * 
     * @return student.html
     * @date Sat Jun 16 13:36:48 2018
     */
    public function add(){
        return $this->fetch();
    }

    /**
     * 插入学生信息
     * 
     * @return error/success
     * @date Sat Jun 16 13:42:04 2018
     */
    public function insert()
    {
        $post                 = $this->req->post();
        $data['stu_name']      = $post['name'];
        $data['stu_login']      = $post['login'];
        $data['stu_subname']   = $post['subname'];
        $data['stu_password']  = $post['password'];
        $data['stu_sex']       = $post['sex'];
        $data['stu_entertime'] = $post['enter'];
        $data['stu_endtime']   = $post['end'];
        $data['stu_tel']       = $post['tel'];
        $data['stu_email']     = $post['email'];
        $data['stu_qq']        = $post['qq'];
        $data['stu_address']   = $post['address'];
        $data['stu_createtime']      = time();
        $model = new StudentModel;
        $result = $model->where(['stu_login'=>$post['login']])->find();
        if($result){
            return $this->error('已存在同名的登录账号');
        } else{
            $result = $model->save($data);
            if($result){
                return $this->success('插入成功');
            }else{
                return $this->error('插入失败');
            }
        }
    }

    public function edit(){
        if($this->req->has('id')){
            $id = $this->req->get('id');
            $model = new \app\admin\model\Student;
            $data = $model->get(['stu_id'=>$id]);
            if($data){
                $this->assign('data',$data);
                return $this->fetch();
            }else{
                return $this->error('没有数据');
            }
        }
    }

    public function update(){
        $post = $this->req->post();
        // var_dump($post);exit();
        $rule = [
            'id' => 'require',
            'name' => 'require',
            'entertime' => 'require',
            'endtime'=> 'require',
            'password' => 'require',
            'sex' => 'require'
        ];
        $validate = new Validate($rule);
        $tmp = [];
        if($validate->check($post)){
            foreach($post as $key=>$v){
                $tmp['stu_'.$key] = $v;
            }
            $model = new \app\admin\model\Student;
            $result = $model->where(['stu_login'=>$post['login']])->find();
            if($result){
                $info['msg'] = '已存在同名的登录账号';
                return $this->wrong($info);
            } else {
                $result = $model->where(['stu_id'=>$post['id']])->update($tmp);
                if($result){
                    $info = [
                        'msg' => '更新成功',
                    ];
                    return $this->right($info);
                }else{
                    $info['msg'] = '更新失败';
                    return $this->wrong($info);
                }
            }
        } else {
            $info['msg'] = '请把所需数据填写完整';
            return $this->wrong($info);
        }
                
    }
    public function remove(){
        $post = $this->req->post();
        // var_dump($post);exit();
        $rule = [
            'id' => 'require|number'
        ];
        $validate = new Validate($rule);
        if($validate->check($post)){
            $model = new StudentModel;
            // $student = $model->destroy(['stu_id'=>$post['id']]);
            $student = 1;
            if($student){
                $info['msg'] = '删除成功';
                echo $this->right($info);
            }else{
                $info['msg'] = "删除失败";
                echo $this->wrong($info);
            }
        }else{
            $info['msg'] = '系统错误';
            echo $this->wrong($info);
        }
        return;
    }

    public function state(){
        $post = $this->req->post();
        // var_dump($post);exit();
        $rule = [
            'id' => 'require|number',
            'state' => 'require|number'
        ];
        $validate = new Validate($rule);
        if($validate->check($post)){
            $model = new StudentModel;
            $result = $model->where(['stu_id'=>$post['id']])->update(['stu_state'=>$post['state']]);
            // $student = 1;
            if($result){
                $info['msg'] = "修改成功";
                $info['state'] = 1; 
            }else{
                $info['msg'] = '修改失败';
                $info['state'] = 0; 
            }
        }else{
            $info['msg'] = '系统错误';
            $info['state'] = 0; 
        }
        return $this->send($info);
    }     
}
